package cn.itcast.wushangxiantu.BaseAdapter;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.MessageEvent;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import cn.itcast.wushangxiantu.UserData.JiaoYisuo;
import cn.itcast.wushangxiantu.UserData.User;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class JiaoYiSuoZhuangbeiAdapter extends BaseExpandableListAdapter {
    private Context context;
    private static JiaoYisuo jiaoYisuo;
    int num1;
    int num2;
    public JiaoYiSuoZhuangbeiAdapter(Context context) {
        this.context = context;
    }

    public static JiaoYisuo getJiaoYisuo() {
        return jiaoYisuo;
    }

    public void setJiaoYisuo(JiaoYisuo jiaoYisuo) {
        this.jiaoYisuo = jiaoYisuo;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return jiaoYisuo.getWP().size();
    }

    @Override
    public int getChildrenCount(int i) {
        return jiaoYisuo.getWP().get(i).getWp().size();
    }

    @Override
    public Object getGroup(int i) {
        return jiaoYisuo.getWP().get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return jiaoYisuo.getWP().get(i).getWp().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.expand_1, null);
        ViewHolder1 viewHolder1 = new ViewHolder1(view);
        viewHolder1.tvName.setText(jiaoYisuo.getWP().get(i).getWp().get(0).getName());
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.adapter_jiaoyisuo_zhuangbei, null);
        ViewHolder2 viewHolder2 = new ViewHolder2(view);
        String shuzhi1="";
        String shuzhi2="";
        switch (jiaoYisuo.getWP().get(i).getWp().get(i1).getRegion()){
            case 2:
                shuzhi1 += "攻击力：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * 40 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 50))) ;
               if( ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                       jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000)))>45){
                   shuzhi2 += "暴击率：45" ;
               }else {
                   shuzhi2 += "暴击率：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                           jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000))) ;
               }
                break;
            case 3:
                shuzhi1 += "生命：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * 100 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 50))) ;
                shuzhi2 += "防御力：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * 20 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 100))) ;
                break;
            case 4:
                if (((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000)))>45){
                    shuzhi1 += "命中：45";
                }else {
                    shuzhi1 += "命中：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                            jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000))) ;

                }
               shuzhi2 += "穿透：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * 10 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 50))) ;
                break;
            case 5:
                if (((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000)))>45){
                    shuzhi1 += "闪避：45";
                }else {
                    shuzhi1 += "闪避：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() / 5 +
                            jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 1000))) ;

                }
               shuzhi2 += "恢复：" + ((int) (jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * 10 +
                        jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj() * (double) (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz() / 50))) ;
                break;
            case 7:
                shuzhi1 += "阶级：" + jiaoYisuo.getWP().get(i).getWp().get(i1).getPz();
                shuzhi2 += "等级：" +jiaoYisuo.getWP().get(i).getWp().get(i1).getJsdj();
                break;
        }
        viewHolder2.tvPrice.setText("售价："+jiaoYisuo.getWP().get(i).getWp().get(i1).getPrice());
        viewHolder2.tvShuZhi1.setText(shuzhi1);
        viewHolder2.tvShuZhi2.setText(shuzhi2);
        if (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz()>290){
            viewHolder2.tvPrice.setTextColor(ContextCompat.getColor(context,R.color.wuqi4));
            viewHolder2.tvShuZhi1.setTextColor(ContextCompat.getColor(context,R.color.wuqi4));
            viewHolder2.tvShuZhi2.setTextColor(ContextCompat.getColor(context,R.color.wuqi4));
        }else if (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz()>260){
            viewHolder2.tvPrice.setTextColor(ContextCompat.getColor(context,R.color.wuqi3));
            viewHolder2.tvShuZhi1.setTextColor(ContextCompat.getColor(context,R.color.wuqi3));
            viewHolder2.tvShuZhi2.setTextColor(ContextCompat.getColor(context,R.color.wuqi3));
        }else if (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz()>200){
            viewHolder2.tvPrice.setTextColor(ContextCompat.getColor(context,R.color.wuqi2));
            viewHolder2.tvShuZhi1.setTextColor(ContextCompat.getColor(context,R.color.wuqi2));
            viewHolder2.tvShuZhi2.setTextColor(ContextCompat.getColor(context,R.color.wuqi2));
        }else if (jiaoYisuo.getWP().get(i).getWp().get(i1).getPz()>1){
            viewHolder2.tvPrice.setTextColor(ContextCompat.getColor(context,R.color.wuqi1));
            viewHolder2.tvShuZhi1.setTextColor(ContextCompat.getColor(context,R.color.wuqi1));
            viewHolder2.tvShuZhi2.setTextColor(ContextCompat.getColor(context,R.color.wuqi1));
        }else {
            viewHolder2.tvPrice.setTextColor(ContextCompat.getColor(context,R.color.heise));
            viewHolder2.tvShuZhi1.setTextColor(ContextCompat.getColor(context,R.color.heise));
            viewHolder2.tvShuZhi2.setTextColor(ContextCompat.getColor(context,R.color.heise));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num1=i;
                num2=i1;
                GeneralDialogZahuo(context,"最大购买数量为"+jiaoYisuo.getWP().get(i).getWp().get(i1).getNumber(),i,i1);
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class ViewHolder1 {
        public View rootView;
        public TextView tvName;

        public ViewHolder1(View rootView) {
            this.rootView = rootView;
            this.tvName = (TextView) rootView.findViewById(R.id.tvName);
        }

    }

    class ViewHolder2 {
        public View rootView;
        public TextView tvShuZhi1;
        public TextView tvShuZhi2;
        public TextView tvPrice;

        public ViewHolder2(View rootView) {
            this.rootView = rootView;
            this.tvShuZhi1 = (TextView) rootView.findViewById(R.id.tvShuZhi1);
            this.tvShuZhi2 = (TextView) rootView.findViewById(R.id.tvShuZhi2);
            this.tvPrice = (TextView) rootView.findViewById(R.id.tvPrice);
        }

    }
    private void GeneralDialogZahuo(final Context context, String title, final int num1, final int num2) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("")){
                            dialogInterface.dismiss();
                            ToastUtil.toast(context,"输入有误");
                            return;
                        }
                        int chushouNum=Integer.parseInt(editText.getText().toString().trim());
                        int cunzaiNum=jiaoYisuo.getWP().get(num1).getWp().get(num2).getNumber();
                        int bag_id;

                        if (chushouNum < 1||chushouNum>cunzaiNum) {
                            ToastUtil.toast(context,"数量有误");
                        } else {
                           //向服务器发送请求
                            getDate(chushouNum,jiaoYisuo.getWP().get(num1).getWp().get(num2).getId());
                        }
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void getDate(int number,int id) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&id=" + id + "&need=jiaoyisuogoumai" + "&number=" + number, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("goumai1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("goumai2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                }catch (Exception e){

                }
                Message message = new Message();
                message.obj = user;
                message.what=1;
                handler.sendMessage(message);
            }
        });

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what==1){
                User user= (User) message.obj;
               if (user!=null){
                   if (user.getStw().equals("OK")){
                       //执行修改数据
                       Login.getUser().setMoney(user.getMoney());
                       if (user.getNumber()==0){
                           if (jiaoYisuo.getWP().get(num1).getWp().size()==1){
                               jiaoYisuo.getWP().remove(num1);
                           }else{
                               jiaoYisuo.getWP().get(num1).getWp().remove(num2);
                           }
                       }else{
                           jiaoYisuo.getWP().get(num1).getWp().get(num2).setNumber(user.getNumber());
                       }
                       ToastUtil.toast(context,"购买成功");
                       EventBus.getDefault().post(new MessageEvent("YouXiang"));
                       EventBus.getDefault().post(new MessageEvent("setmoney"));
                       notifyDataSetChanged();
                   }else{
                       ToastUtil.toast(context,user.getStw());
                   }
               }else {
                   ToastUtil.toast(context,"服务器坏掉了");
               }
            }
            return false;
        }
    });
}
